--- testSocket
-- @module asyncSocket
-- @author AIRM2M
-- @license MIT
-- @copyright openLuat.com
-- @release 2018.10.27
require "socket"
require "ntp"
require "misc"
require "utils"
require "pm"
require "http"
require "iconv"
require "common"
require "bit"
require"pins"

require"nvm"
require"pins"
require "sim"
require"update"
require"http"
module(..., package.seeall)
--  初始化nvm---
-- 此处的IP和端口请填上你自己的socket服务器和端口
local ip, port = "183.230.40.40", "1811"
local setGpio18Fnc = pins.setup(pio.P0_18,1)
local setGpio19Fnc = pins.setup(pio.P0_19,1)
local tcp_enable="1"

local Tflag=false
local clientConnected=false
-- 异步接口演示代码
local asyncClient
sys.taskInit(
    function()
        while true do
            if tcp_enable=="1" then
                while not socket.isReady() do
                    log.info("reday")
                    sys.wait(1000)
                end
                asyncClient = socket.tcp()
                while not asyncClient:connect(ip, port) do
                    log.info("未连接")
                    sys.wait(2000)
                end
                log.info("连接")
                clientConnected=true
                Tflag=true
                while asyncClient:asyncSelect(230) do
                    log.info("心跳包")
                    sys.wait(1000)
                end
                log.info("关闭")
                clientConnected = false
                asyncClient:close()
            end
            sys.wait(1000)
        end
    end
)

local tf=false
local DATATIME
-- 测试代码,用于发送消息给socket
sys.taskInit(
    function()
        while not socket.isReady() do
            sys.wait(2000)
        end
        sys.wait(10000)
        while true do

            if Tflag then
                Tflag = false
                asyncClient:asyncSend(string.rep("*443657#1478963#sample*\r\n", 1))
                log.info("发送")
            end
            if tf then
                tf=false
                asyncClient:asyncSend(string.rep("0,", 1))
            end
            sys.wait(500)
        end
        -- 这是演示用异步接口发送数据
    end
)
local year,month,day,hou,minute,sencode,time
local stop_year,stop_month,stop_day,stop_hou,stop_minute,stop_sencode,stop_time
local start_flag=false;
local count=0
sys.taskInit(
    function ()
        while true do
            while start_flag do
                count=count+1
                sys.wait(10000)
                if count>=6*time then
                    start_flag=false
                    setGpio18Fnc(1)
                    setGpio19Fnc(1)
                    asyncClient:asyncSend(string.rep("0,", 1))
                    count=0
                end
            end
            sys.wait(500)
        end
    end
)

-- 测试代码,用于从socket接收消息
sys.taskInit(
    function()
        while not socket.isReady() do-------等待socket准备完成--------
           
            sys.wait(2000)
        end
        -- 接收服务器数据
        while true do
            log.info("ostime",os.time()+10)
            if clientConnected then
  --             DATATIME= string.format("%d", tm.hour+5)
  --             log.info(DATATIME)
                local data = asyncClient:asyncRecv()-------接收服务器数据---------
                if data~="" then--------是否接收到服务器数据-----------
                    log.info("这是服务器下发数据:", data)
                    if data=="auth" then
                        tf=true
                    end
                    local tjsondata,result,errinfo=json.decode(data)--------解析json数据--------
                    if result and type(tjsondata)=="table" then   ------------是否正确解析--------------
                        if string.find(tjsondata["SET"],"START")~=nil then
                            log.info("123")                           
                            asyncClient:asyncSend(string.rep("1,", 1))
                            start_flag=true
                            setGpio18Fnc(0)
                            setGpio19Fnc(0)
                            time=tjsondata["TIME"]
                        end
                        if string.find(tjsondata["SET"],"STOP")~=nil then
                            start_flag=false
                            setGpio18Fnc(1)
                            setGpio19Fnc(1)
                            count=0
                            asyncClient:asyncSend(string.rep("0,", 1))
                        end
                    end
                    
                end
            end

            sys.wait(1000)
        end
    end
)
ntp.timeSync()

